package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl  implements AddressBookService {


    @Autowired
    private AddressBookMapper addressBookMapper;
    /**
     * 查询地址
     * @return
     */
    @Override
    public List<AddressBook> list(AddressBook addressBook) {
        return addressBookMapper.list(addressBook);
    }

    /**
     * 新增地址
     * @param addressBook
     */
    @Override
    public void add(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.add(addressBook);
    }

    /**
     * 数据回显
     * @param id
     * @return
     */
    @Override
    public AddressBook findByid(Long id) {
        return addressBookMapper.findByid(id);
    }

    /**
     * 修改信息
     * @param addressBook
     */
    @Override
    public void updateMessage(AddressBook addressBook) {
        addressBookMapper.updateMessage(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook
     */
    @Override
    public void setDefault(AddressBook addressBook) {
//        先把全部都设置非默认
        addressBook.setIsDefault(0);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.setAlldefault(addressBook);
//        再把一个设置默认地址
        AddressBook addressBook1 = addressBookMapper.findByid(addressBook.getId());
        addressBook1.setIsDefault(1);
        addressBookMapper.updateMessage(addressBook1);

    }

    /**
     * 删除地址
     * @param id
     */
    @Override
    public void deletes(Long id) {
        addressBookMapper.deletes(id);
    }


}
